package com.app.command;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.app.dao.BaseDAO;

public abstract class Command {
	
	protected BufferedReader getCommandReader() throws IOException{
		InputStreamReader reader = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(reader);
		return br;
	}
	
	public CommandResult executeCommand(){
		CommandResult result = null;
		try{
			BaseDAO.beginTransaction();
			result = execute(getCommandReader());
			BaseDAO.commiteTransaction();
		}catch (Exception e) {
			BaseDAO.rollbackTransaction();
			result = new CommandResult(CommandResultStatus.FAILED);
			result.setMessage(e.getMessage());
		}
		return result;
	}
	
	protected abstract CommandResult execute(BufferedReader dataLineReader) throws Exception;
}